/**
 * Copyright (c)  HeiFeiDeFengZheng.Co.Ltd. All rights reserved.
 **/
package com.defu.newmall.service.impl;

import com.defu.newmall.common.PageResult;
import com.defu.newmall.dao.GoodsCategoryDao;
import com.defu.newmall.entity.GoodsCategory;
import com.defu.newmall.service.NewBeeMallCategoryService;
import com.defu.newmall.utils.PageQueryUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.groupingBy;

/**
 * 功能描述：
 *
 * @author huifeidefengzheng
 * @since 2024-03-11
 *
 **/
@Service
public class NewBeeMallCategoryServiceImpl implements NewBeeMallCategoryService {


    @Autowired
    private GoodsCategoryDao goodsCategoryDao;

    @Override
    public String saveCategory(GoodsCategory goodsCategory) {
        GoodsCategory temp = goodsCategoryDao.selectByLevelAndName(goodsCategory.getCategoryLevel(), goodsCategory.getCategoryName());
        if (temp != null) {
            return "未查询到记录！";
        }
        if (goodsCategoryDao.insertSelective(goodsCategory) > 0) {
            return "success";
        }
        return "database error";
    }

    @Override
    public String updateGoodsCategory(GoodsCategory goodsCategory) {
        GoodsCategory temp = goodsCategoryDao.selectByPrimaryKey(goodsCategory.getCategoryId());
        if (temp == null) {
            return "未查询到记录！";
        }
        GoodsCategory temp2 = goodsCategoryDao.selectByLevelAndName(goodsCategory.getCategoryLevel(), goodsCategory.getCategoryName());
        if (temp2 != null && !temp2.getCategoryId().equals(goodsCategory.getCategoryId())) {
            //同名且不同id 不能继续修改
            return "已存在同级同名的分类！";
        }
        goodsCategory.setUpdateTime(new Date());
        if (goodsCategoryDao.updateByPrimaryKeySelective(goodsCategory) > 0) {
            return "success";
        }
        return "database error";
    }

    @Override
    public GoodsCategory getGoodsCategoryById(Long id) {
        return goodsCategoryDao.selectByPrimaryKey(id);
    }

    @Override
    public Boolean deleteBatch(Long[] ids) {
        if (ids.length < 1) {
            return false;
        }
        //删除分类数据
        return goodsCategoryDao.deleteBatch(ids) > 0;
    }

    @Override
    public PageResult getCategorisPage(PageQueryUtil pageUtil) {
        List<GoodsCategory> goodsCategories = goodsCategoryDao.findGoodsCategoryList(pageUtil);
        int total = goodsCategoryDao.getTotalGoodsCategories(pageUtil);
        PageResult pageResult = new PageResult(goodsCategories, total, pageUtil.getLimit(), pageUtil.getPage());
        return pageResult;
    }

    @Override
    public List<GoodsCategory> selectByLevelAndParentIdsAndNumber(List<Long> parentIds, int categoryLevel) {
        return goodsCategoryDao.selectByLevelAndParentIdsAndNumber(parentIds, categoryLevel, 0);//0代表查询所有
    }
}
